United States Car Accident Project

Author

Jason Rappazzo

Published

April 19, 2023

1 Indroduction

1.1 Motivation for the Project - James

1.2 Research Questions -James

1.3 Overview of Modeling Techniques - Me

Binary Response Model

Linear Regression

Lasso Regression

Ridge Regression

Ordinal Logit Regression

Decision Tree

Random Forest

Artificial Neural Network

2 Raw Data

3 Preparing Data For Machine Learning

accident_12var <- accident_raw %>% 
  select(Severity,State, `Temperature(F)`, `Humidity(%)`,
         `Visibility(mi)`, `Wind_Speed(mph)`, Weather_Condition,
         `Precipitation(in)`, Crossing, Junction, Traffic_Signal,
         Sunrise_Sunset)

colnames(accident_12var) <- gsub("\\)|\\%|\\(", ".", colnames(accident_12var))
library(caret)
library(recipes)
library(dplyr)

# Split the data into training and testing sets
set.seed(2)
train_indices <- createDataPartition(accident_12var$Severity, p = 0.8, list = FALSE)
train_set <- accident_12var[train_indices, ]
test_set <- accident_12var[-train_indices, ]


# TRAIN SET

# Make a copy of the train set
copied_traindata <- data.frame(train_set)

# Add an id column to copied_traindata
copied_traindata <- copied_traindata %>% mutate(id = row_number())

# Separate Label from Feature
accident <- select(copied_traindata, -Severity) # drop Severity column
label <- copied_traindata$Severity # select Severity column

# Separate Numerical from Categorical
accident_num <- accident %>% 
  select(id, Temperature.F., Humidity..., Visibility.mi., Wind_Speed.mph., Precipitation.in.)

accident_cat <- accident %>% 
  select(id, State, Weather_Condition, Crossing, Junction, Traffic_Signal, Sunrise_Sunset)

# Define numeric and categorical attributes
num_attribs <- names(accident_num)[2:6]
cat_attribs <- names(accident_cat)[2:7]

# Define preprocessing pipelines
num_pipeline <- recipe(~., data = accident_num) %>%
  step_impute_median(all_numeric(), -has_role("id")) %>%
  step_center(all_numeric(), -has_role("id")) %>%
  step_scale(all_numeric(), -has_role("id"))

cat_pipeline <- recipe(~., data = accident_cat) %>%
  step_dummy(all_nominal())

# Merge the preprocessed numerical and categorical features into a single dataset

accident <- accident %>% rename(Index = id) 

df1 <- mutate(num_pipeline %>% prep() %>% bake(new_data = NULL), join_key = "Index")
df2 <- mutate(cat_pipeline %>% prep() %>% bake(new_data = NULL), join_key = "Index")

accident_prepared <- accident %>% 
  select(-one_of(c(cat_attribs, num_attribs)))

accident_prepared <- cbind(accident_prepared, df1,df2)



accident_prepared <- accident_prepared %>% 
  distinct()

accident_prepared <- select(accident_prepared, -c("Index", "id", "join_key", "id.1", "join_key.1"))






#TEST SET
# Make a copy of the test set
copied_testdata <- data.frame(test_set)

# Add an id column to copied_testdata
copied_testdata <- copied_testdata %>% mutate(id = row_number())

# Separate Label from Feature
accident_test <- select(copied_testdata, -Severity) # drop Severity column
label_test <- copied_testdata$Severity # select Severity column

# Separate Numerical from Categorical
accident_num_test <- copied_testdata %>% 
  select(Temperature.F., Humidity..., Visibility.mi., Wind_Speed.mph., Precipitation.in.)

accident_cat_test <- copied_testdata %>% 
  select(State, Weather_Condition, Crossing, Junction, Traffic_Signal, Sunrise_Sunset)

# Define numeric and categorical attributes
num_attribs <- names(accident_num_test)[1:6]
cat_attribs <- names(accident_cat_test)[1:7]

# Define preprocessing pipelines
num_pipeline <- recipe(~., data = accident_num_test) %>%
  step_impute_median(all_numeric(), -has_role("id")) %>%
  step_center(all_numeric(), -has_role("id")) %>%
  step_scale(all_numeric(), -has_role("id"))

cat_pipeline <- recipe(~., data = accident_cat_test) %>%
  step_dummy(all_nominal())

# Merge the preprocessed numerical and categorical features into a single dataset

copied_testdata <- copied_testdata %>% rename(Index = id) 

df1 <- mutate(num_pipeline %>% prep() %>% bake(new_data = NULL), join_key = "Index")
df2 <- mutate(cat_pipeline %>% prep() %>% bake(new_data = NULL), join_key = "Index")

accident_prepared_test <- accident_test %>% 
  select(-one_of(c(cat_attribs, num_attribs)))

accident_prepared_test <- cbind(accident_prepared_test, df1,df2)



accident_prepared_test <- accident_prepared_test %>% 
  distinct()

accident_prepared_test <- select(accident_prepared_test, -c("id", "join_key", "join_key.1"))

4 Models

4.1 Linear Regression

# Fit the linear regression model
lin_reg <- lm(label ~ ., data = accident_prepared)

# Use the model to predict the response variable using the test data
y_pred <- predict(lin_reg, newdata = accident_prepared_test)

# Calculate the residuals
residuals <- y_pred - label_test

# Calculate the squared errors
squared_errors <- residuals^2

# Calculate the mean squared error
mse <- mean(squared_errors)

# Print the MSE
cat("MSE:", mse)
## MSE: 0.1324914

4.2 Ridge Regression

#ridge regression

library(glmnet)

# Separate the predictor variables from the response variable
y <- label
X <- as.matrix(select(accident_prepared, -label))

# Define the lambda sequence for ridge regression
lambda_seq <- 10^seq(10, -2, length = 100)

# Perform cross-validated ridge regression
ridge_fit <- cv.glmnet(X, y, alpha = 0, lambda = lambda_seq)

# Plot the cross-validation results
plot(ridge_fit)


ridge_coef <- coef(ridge_fit)[-1]

y_pred <- predict(ridge_fit, newx = X)

mse <- mean((y - y_pred)^2)


# Print the MSE
cat("MSE:", mse)
## MSE: 0.1341102

4.3 Lasso Regression

x <- model.matrix(~ ., data = accident_prepared) 
y <- label

# Fit a Lasso regression with cross-validation
lasso_model <- cv.glmnet(x, y, alpha = 1) 

extra_columns <- setdiff(colnames(accident_prepared_test), colnames(accident_prepared))

accident_prepared_test <- accident_prepared_test %>%
                          select(-one_of(extra_columns))


# Predict the response variable using the test data
x_test <- model.matrix(~ ., data = accident_prepared_test) 
y_pred <- predict(lasso_model, newx = x_test)

# Calculate the MSE
mse <- mean((y_pred - label_test)^2)

# Print the MSE
cat("MSE:", mse)
## MSE: 0.134835


plot(lasso_model)

5 Results

5.1 Linear Regression Results


summary(lin_reg)
## 
## Call:
## lm(formula = label ~ ., data = accident_prepared)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.52797 -0.06866 -0.02075 -0.00465  2.15885 
## 
## Coefficients:
##                                                  Estimate Std. Error t value
## (Intercept)                                     2.0958311  0.1388593  15.093
## Temperature.F.                                  0.0016274  0.0018751   0.868
## Humidity...                                     0.0068859  0.0018842   3.655
## Visibility.mi.                                 -0.0015936  0.0019652  -0.811
## Wind_Speed.mph.                                -0.0008635  0.0016127  -0.535
## Precipitation.in.                              -0.0001594  0.0015382  -0.104
## CrossingTRUE                                   -0.0362585  0.0043024  -8.428
## JunctionTRUE                                    0.0189544  0.0203361   0.932
## Traffic_SignalTRUE                             -0.0141038  0.0041651  -3.386
## State_AR                                        0.1504189  0.0298089   5.046
## State_AZ                                       -0.1990743  0.0206304  -9.650
## State_CA                                       -0.1041589  0.0185139  -5.626
## State_CO                                        0.5539580  0.0290738  19.054
## State_CT                                        0.5027875  0.0315755  15.923
## State_DC                                        0.1494364  0.0283580   5.270
## State_DE                                        0.3747692  0.0404234   9.271
## State_FL                                       -0.0966262  0.0185361  -5.213
## State_GA                                        0.6010876  0.0300341  20.014
## State_IA                                        0.3997343  0.0386140  10.352
## State_ID                                        0.0055216  0.0290426   0.190
## State_IL                                        0.2903336  0.0222829  13.029
## State_IN                                        0.5940194  0.0326600  18.188
## State_KS                                        0.1121830  0.0723924   1.550
## State_KY                                        0.0240924  0.0414291   0.582
## State_LA                                       -0.1186637  0.0199758  -5.940
## State_MA                                        0.5383176  0.0558111   9.645
## State_MD                                        0.1930208  0.0217256   8.884
## State_ME                                       -0.1622222  0.0932900  -1.739
## State_MI                                        0.0838205  0.0219068   3.826
## State_MN                                       -0.1049052  0.0201320  -5.211
## State_MO                                        0.0057190  0.0271965   0.210
## State_MS                                        0.1352351  0.0477571   2.832
## State_MT                                       -0.0942080  0.0233438  -4.036
## State_NC                                       -0.0175734  0.0192985  -0.911
## State_ND                                       -0.1191537  0.0611300  -1.949
## State_NE                                        0.3107596  0.0585094   5.311
## State_NH                                        0.6174619  0.0854836   7.223
## State_NJ                                        0.2714963  0.0233061  11.649
## State_NM                                       -0.1007116  0.0877186  -1.148
## State_NV                                        0.1959995  0.0512287   3.826
## State_NY                                        0.1571378  0.0201787   7.787
## State_OH                                        0.0299424  0.0240281   1.246
## State_OK                                       -0.0075929  0.0256918  -0.296
## State_OR                                       -0.0526093  0.0193208  -2.723
## State_PA                                        0.0116459  0.0192052   0.606
## State_RI                                        0.1094582  0.1226954   0.892
## State_SC                                       -0.0984665  0.0190376  -5.172
## State_SD                                        0.0784433  0.1112357   0.705
## State_TN                                       -0.0866081  0.0202542  -4.276
## State_TX                                       -0.0933029  0.0191659  -4.868
## State_UT                                       -0.0573579  0.0221747  -2.587
## State_VA                                        0.0707872  0.0197404   3.586
## State_VT                                       -0.2356675  0.1168900  -2.016
## State_WA                                        0.2417323  0.0240870  10.036
## State_WI                                        1.4296343  0.0548606  26.059
## State_WV                                        0.0655956  0.0427008   1.536
## Weather_Condition_Blowing.Dust...Windy          0.0227432  0.2917855   0.078
## Weather_Condition_Blowing.Snow...Windy         -0.1746618  0.3891952  -0.449
## Weather_Condition_Clear                         0.2725820  0.2031507   1.342
## Weather_Condition_Cloudy                        0.0146426  0.1377077   0.106
## Weather_Condition_Cloudy...Windy                0.0284363  0.1395128   0.204
## Weather_Condition_Drizzle                       0.1355011  0.1495017   0.906
## Weather_Condition_Drizzle.and.Fog              -0.0681430  0.2282171  -0.299
## Weather_Condition_Fair                          0.0196473  0.1376459   0.143
## Weather_Condition_Fair...Windy                  0.0515160  0.1386097   0.372
## Weather_Condition_Fog                           0.0073467  0.1380526   0.053
## Weather_Condition_Fog...Windy                  -0.0065058  0.2026255  -0.032
## Weather_Condition_Freezing.Drizzle             -0.1830741  0.3890729  -0.471
## Weather_Condition_Haze                          0.0104004  0.1381591   0.075
## Weather_Condition_Haze...Windy                  0.0567806  0.1572274   0.361
## Weather_Condition_Heavy.Drizzle                -0.0571070  0.2512489  -0.227
## Weather_Condition_Heavy.Rain                    0.0077773  0.1401272   0.056
## Weather_Condition_Heavy.Rain...Windy            0.1962829  0.1796991   1.092
## Weather_Condition_Heavy.Sleet                   0.0302063  0.3890860   0.078
## Weather_Condition_Heavy.Snow                    0.0381202  0.1529729   0.249
## Weather_Condition_Heavy.Snow...Windy            0.0174797  0.2920136   0.060
## Weather_Condition_Heavy.T.Storm                -0.0076763  0.1420776  -0.054
## Weather_Condition_Heavy.T.Storm...Windy        -0.1646998  0.1838617  -0.896
## Weather_Condition_Light.Drizzle                 0.0395235  0.1406483   0.281
## Weather_Condition_Light.Drizzle...Windy         0.6588084  0.2134850   3.086
## Weather_Condition_Light.Freezing.Drizzle       -0.0536470  0.2132135  -0.252
## Weather_Condition_Light.Freezing.Rain           0.2522939  0.1835475   1.375
## Weather_Condition_Light.Freezing.Rain...Windy   0.8712753  0.2282335   3.817
## Weather_Condition_Light.Ice.Pellets             0.8135605  0.2918535   2.788
## Weather_Condition_Light.Rain                    0.0286741  0.1378294   0.208
## Weather_Condition_Light.Rain...Windy            0.0573729  0.1425537   0.402
## Weather_Condition_Light.Rain.Shower             0.1432256  0.2285365   0.627
## Weather_Condition_Light.Rain.with.Thunder      -0.0073404  0.1401625  -0.052
## Weather_Condition_Light.Snow                    0.0668484  0.1382771   0.483
## Weather_Condition_Light.Snow...Windy            0.0484749  0.1476732   0.328
## Weather_Condition_Light.Snow.and.Sleet         -0.0109237  0.3890494  -0.028
## Weather_Condition_Light.Snow.Shower            -0.0225435  0.3892686  -0.058
## Weather_Condition_Light.Thunderstorms.and.Rain  0.6127244  0.3892606   1.574
## Weather_Condition_Mist                          0.1233196  0.1731992   0.712
## Weather_Condition_Mostly.Cloudy                 0.0058899  0.1377034   0.043
## Weather_Condition_Mostly.Cloudy...Windy         0.0218726  0.1399015   0.156
## Weather_Condition_N.A.Precipitation            -0.0913070  0.1545100  -0.591
## Weather_Condition_Overcast                      0.4983898  0.1431143   3.482
## Weather_Condition_Partly.Cloudy                 0.0158153  0.1377180   0.115
## Weather_Condition_Partly.Cloudy...Windy        -0.0037962  0.1423538  -0.027
## Weather_Condition_Patches.of.Fog               -0.0027372  0.1534136  -0.018
## Weather_Condition_Rain                          0.0142572  0.1385239   0.103
## Weather_Condition_Rain...Windy                  0.0358286  0.1623339   0.221
## Weather_Condition_Scattered.Clouds             -0.1571482  0.2512462  -0.625
## Weather_Condition_Shallow.Fog                  -0.0550136  0.1512079  -0.364
## Weather_Condition_Showers.in.the.Vicinity      -0.0797286  0.2512444  -0.317
## Weather_Condition_Sleet                        -0.2149288  0.2918647  -0.736
## Weather_Condition_Smoke                         0.1307071  0.1395502   0.937
## Weather_Condition_Smoke...Windy                 0.9997957  0.2917851   3.426
## Weather_Condition_Snow                          0.0810832  0.1427000   0.568
## Weather_Condition_Snow...Windy                 -0.0018894  0.1947696  -0.010
## Weather_Condition_Snow.and.Sleet               -0.1189619  0.3890803  -0.306
## Weather_Condition_T.Storm                       0.0249463  0.1397750   0.178
## Weather_Condition_T.Storm...Windy              -0.0160320  0.2511966  -0.064
## Weather_Condition_Thunder                       0.0108711  0.1396562   0.078
## Weather_Condition_Thunder...Windy              -0.0253296  0.1761665  -0.144
## Weather_Condition_Thunder...Wintry.Mix         -0.2531879  0.3897783  -0.650
## Weather_Condition_Thunder.and.Hail              0.0035043  0.3890103   0.009
## Weather_Condition_Thunder.in.the.Vicinity       0.0334562  0.1390793   0.241
## Weather_Condition_Widespread.Dust               0.0045709  0.3890005   0.012
## Weather_Condition_Widespread.Dust...Windy       0.0209412  0.3890040   0.054
## Weather_Condition_Wintry.Mix                    0.0400567  0.1421607   0.282
## Weather_Condition_Wintry.Mix...Windy           -0.0084905  0.3890825  -0.022
## Sunrise_Sunset_Night                            0.0116030  0.0031980   3.628
##                                                Pr(>|t|)    
## (Intercept)                                     < 2e-16 ***
## Temperature.F.                                 0.385444    
## Humidity...                                    0.000258 ***
## Visibility.mi.                                 0.417425    
## Wind_Speed.mph.                                0.592363    
## Precipitation.in.                              0.917449    
## CrossingTRUE                                    < 2e-16 ***
## JunctionTRUE                                   0.351310    
## Traffic_SignalTRUE                             0.000709 ***
## State_AR                                       4.52e-07 ***
## State_AZ                                        < 2e-16 ***
## State_CA                                       1.85e-08 ***
## State_CO                                        < 2e-16 ***
## State_CT                                        < 2e-16 ***
## State_DC                                       1.37e-07 ***
## State_DE                                        < 2e-16 ***
## State_FL                                       1.86e-07 ***
## State_GA                                        < 2e-16 ***
## State_IA                                        < 2e-16 ***
## State_ID                                       0.849215    
## State_IL                                        < 2e-16 ***
## State_IN                                        < 2e-16 ***
## State_KS                                       0.121229    
## State_KY                                       0.560884    
## State_LA                                       2.86e-09 ***
## State_MA                                        < 2e-16 ***
## State_MD                                        < 2e-16 ***
## State_ME                                       0.082056 .  
## State_MI                                       0.000130 ***
## State_MN                                       1.88e-07 ***
## State_MO                                       0.833447    
## State_MS                                       0.004631 ** 
## State_MT                                       5.45e-05 ***
## State_NC                                       0.362505    
## State_ND                                       0.051277 .  
## State_NE                                       1.09e-07 ***
## State_NH                                       5.13e-13 ***
## State_NJ                                        < 2e-16 ***
## State_NM                                       0.250922    
## State_NV                                       0.000130 ***
## State_NY                                       6.93e-15 ***
## State_OH                                       0.212717    
## State_OK                                       0.767584    
## State_OR                                       0.006472 ** 
## State_PA                                       0.544256    
## State_RI                                       0.372335    
## State_SC                                       2.32e-07 ***
## State_SD                                       0.480689    
## State_TN                                       1.90e-05 ***
## State_TX                                       1.13e-06 ***
## State_UT                                       0.009694 ** 
## State_VA                                       0.000336 ***
## State_VT                                       0.043788 *  
## State_WA                                        < 2e-16 ***
## State_WI                                        < 2e-16 ***
## State_WV                                       0.124501    
## Weather_Condition_Blowing.Dust...Windy         0.937872    
## Weather_Condition_Blowing.Snow...Windy         0.653594    
## Weather_Condition_Clear                        0.179674    
## Weather_Condition_Cloudy                       0.915320    
## Weather_Condition_Cloudy...Windy               0.838490    
## Weather_Condition_Drizzle                      0.364753    
## Weather_Condition_Drizzle.and.Fog              0.765255    
## Weather_Condition_Fair                         0.886498    
## Weather_Condition_Fair...Windy                 0.710145    
## Weather_Condition_Fog                          0.957560    
## Weather_Condition_Fog...Windy                  0.974386    
## Weather_Condition_Freezing.Drizzle             0.637971    
## Weather_Condition_Haze                         0.939993    
## Weather_Condition_Haze...Windy                 0.717998    
## Weather_Condition_Heavy.Drizzle                0.820197    
## Weather_Condition_Heavy.Rain                   0.955739    
## Weather_Condition_Heavy.Rain...Windy           0.274711    
## Weather_Condition_Heavy.Sleet                  0.938120    
## Weather_Condition_Heavy.Snow                   0.803210    
## Weather_Condition_Heavy.Snow...Windy           0.952268    
## Weather_Condition_Heavy.T.Storm                0.956912    
## Weather_Condition_Heavy.T.Storm...Windy        0.370373    
## Weather_Condition_Light.Drizzle                0.778704    
## Weather_Condition_Light.Drizzle...Windy        0.002030 ** 
## Weather_Condition_Light.Freezing.Drizzle       0.801342    
## Weather_Condition_Light.Freezing.Rain          0.169277    
## Weather_Condition_Light.Freezing.Rain...Windy  0.000135 ***
## Weather_Condition_Light.Ice.Pellets            0.005312 ** 
## Weather_Condition_Light.Rain                   0.835198    
## Weather_Condition_Light.Rain...Windy           0.687343    
## Weather_Condition_Light.Rain.Shower            0.530853    
## Weather_Condition_Light.Rain.with.Thunder      0.958234    
## Weather_Condition_Light.Snow                   0.628786    
## Weather_Condition_Light.Snow...Windy           0.742717    
## Weather_Condition_Light.Snow.and.Sleet         0.977600    
## Weather_Condition_Light.Snow.Shower            0.953819    
## Weather_Condition_Light.Thunderstorms.and.Rain 0.115475    
## Weather_Condition_Mist                         0.476461    
## Weather_Condition_Mostly.Cloudy                0.965883    
## Weather_Condition_Mostly.Cloudy...Windy        0.875763    
## Weather_Condition_N.A.Precipitation            0.554559    
## Weather_Condition_Overcast                     0.000497 ***
## Weather_Condition_Partly.Cloudy                0.908574    
## Weather_Condition_Partly.Cloudy...Windy        0.978725    
## Weather_Condition_Patches.of.Fog               0.985765    
## Weather_Condition_Rain                         0.918025    
## Weather_Condition_Rain...Windy                 0.825319    
## Weather_Condition_Scattered.Clouds             0.531661    
## Weather_Condition_Shallow.Fog                  0.715988    
## Weather_Condition_Showers.in.the.Vicinity      0.750990    
## Weather_Condition_Sleet                        0.461490    
## Weather_Condition_Smoke                        0.348951    
## Weather_Condition_Smoke...Windy                0.000612 ***
## Weather_Condition_Snow                         0.569896    
## Weather_Condition_Snow...Windy                 0.992260    
## Weather_Condition_Snow.and.Sleet               0.759795    
## Weather_Condition_T.Storm                      0.858351    
## Weather_Condition_T.Storm...Windy              0.949112    
## Weather_Condition_Thunder                      0.937954    
## Weather_Condition_Thunder...Windy              0.885673    
## Weather_Condition_Thunder...Wintry.Mix         0.515973    
## Weather_Condition_Thunder.and.Hail             0.992813    
## Weather_Condition_Thunder.in.the.Vicinity      0.809901    
## Weather_Condition_Widespread.Dust              0.990625    
## Weather_Condition_Widespread.Dust...Windy      0.957068    
## Weather_Condition_Wintry.Mix                   0.778120    
## Weather_Condition_Wintry.Mix...Windy           0.982590    
## Sunrise_Sunset_Night                           0.000286 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3638 on 75797 degrees of freedom
## Multiple R-squared:  0.108,  Adjusted R-squared:  0.1065 
## F-statistic: 74.59 on 123 and 75797 DF,  p-value: < 2.2e-16

library(coefplot)
library(broom)


# Extract coefficients and standard errors
coef_df <- tidy(lin_reg, conf.int = TRUE)

# Filter out intercept
coef_df <- coef_df[-1,]

num_coef_df <- coef_df[coef_df$term %in% num_attribs,]
cat_coef_df <- coef_df[grep(".*\\_.*", coef_df$term), ]

# Create plots
plot_num <- ggplot(num_coef_df, aes(x = estimate, y = reorder(term, estimate))) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  labs(x = "Coefficient Estimate", y = "Variable") +
  ggtitle("Linear Regression Results for Numeric Variables") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))+
  geom_vline(xintercept = 0, linetype = "dashed", color = "red")

plot_num



cat_coef_df1 <- cat_coef_df[1:25,]
cat_coef_df2 <- cat_coef_df[25:50,]
cat_coef_df3 <- cat_coef_df[50:75,]
cat_coef_df4 <- cat_coef_df[75:100,]
cat_coef_df5 <- cat_coef_df[100:125,]



# Create  separate plots
plot_cat1 <- ggplot(cat_coef_df1, aes(x = estimate, y = reorder(term, estimate))) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  labs(x = "Coefficient Estimate", y = "Variable") +
  ggtitle("Linear Regression Results for Categorical Variables") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))+
  geom_vline(xintercept = 0, linetype = "dashed", color = "red")

plot_cat1


plot_cat2 <- ggplot(cat_coef_df2, aes(x = estimate, y = reorder(term, estimate))) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  labs(x = "Coefficient Estimate", y = "Variable") +
  ggtitle("Linear Regression Results for Categorical Variables") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))+
  geom_vline(xintercept = 0, linetype = "dashed", color = "red")

plot_cat2


plot_cat3 <- ggplot(cat_coef_df3, aes(x = estimate, y = reorder(term, estimate))) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  labs(x = "Coefficient Estimate", y = "Variable") +
  ggtitle("Linear Regression Results for Categorical Variables") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))+
  geom_vline(xintercept = 0, linetype = "dashed", color = "red")
plot_cat3


plot_cat4 <- ggplot(cat_coef_df4, aes(x = estimate, y = reorder(term, estimate))) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  labs(x = "Coefficient Estimate", y = "Variable") +
  ggtitle("Linear Regression Results for Categorical Variables") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))+
  geom_vline(xintercept = 0, linetype = "dashed", color = "red")

plot_cat4


plot_cat5 <- ggplot(cat_coef_df5, aes(x = estimate, y = reorder(term, estimate))) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
  labs(x = "Coefficient Estimate", y = "Variable") +
  ggtitle("Linear Regression Results for Categorical Variables") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))+
  geom_vline(xintercept = 0, linetype = "dashed", color = "red")

plot_cat5

6 Discussion

7 Conclusion